//
//  HomeController.m
//  Village
//
//  Created by 程虎 on 2017/9/1.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "HomeController.h"
#import "VailtalReferViewController.h"
#import "AnnounNitoceView.h"
#import "LocationTools.h"
#import "HomeHttpTools.h"
#import "HomeView.h"
#import "CompletionView.h"
#import "PolicyHttpTools.h"
#import "PolicyModel.h"

@interface HomeController ()<HomeViewDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) HomeView *homeViews;
@property (nonatomic, strong) LocationTools *locationTools;
@end

@implementation HomeController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.locationTools = [[LocationTools alloc] init];
    [self.locationTools startLocation];
    WeakSelf(weakSelf);
     [self config_UI];
    self.locationTools.locationBlock = ^(LocationModel *model) {
        if (model != nil) {
            [[Globle getInstance] saveLocation:model];
            [weakSelf getAreaid:model];
        }
    };
  
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"首页";
    [[PermissionManager getPermissionManager] requestLocationPermissionBlock:^(BOOL b) {
    }];
    [Tools updatepreson];//更新一下用户信息  避免用户在别的手机上修改 这边得不到及时的反馈
    
    [self updateVersion];
}

#pragma mark - 配置UI
- (void)config_UI
{
    self.homeViews = [[HomeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - 49) SuperView:self];
    self.homeViews.delegate = self;
    [self.scroll addSubview:self.homeViews];
}

#pragma mark - 顶部滚动视图
- (void)scrollNewsClickWithIndex:(NSInteger)index WithData:(VillageModel *)model
{
    if (index == 0) { //banner
        [self addReadNum:model];
    }
    else {
    
    }
}

#pragma mark - 通知公告、村落查询、我的村落
- (void)middleItemsClickWithIndex:(NSInteger)index
{
    if (index == 0) {//通知公告
        if ([Globle getInstance].account.userid != nil) {
            if ([Globle getInstance].isNormal) {
                [Tools ToNextName:@"VailtalReferViewController" Params:nil CurrentVC:self Completion:^(id obj) {
                    
                }];
            }
            else {
                [Tools ToNextName:@"AnnouncementController" Params:nil CurrentVC:self Completion:^(id obj) {
                }];
            }
        }
        else {
            [Tools ToNextName:@"VailtalReferViewController" Params:nil CurrentVC:self Completion:^(id obj) {
                
            }];
        }
    }
    else if (index == 1) {//村落查询
        [Tools ToNextName:@"VailtalReferViewController" Params:nil CurrentVC:self Completion:^(id obj) {
        }];
    }
    else {
        if ([Globle getInstance].account.areaid.length) {
            [Tools ToNextName:@"MyVillageController" Params:nil CurrentVC:self Completion:^(id obj) {
            }];
        }
        else {
            [Tools ToNextName:@"MineVillageViewController" Params:nil CurrentVC:self Completion:^(id obj) {
            }];
        }
    }
}

#pragma mark - 村头条、政策法规
- (void)tableNewsClickWithData:(id )model WithIndex:(NSInteger)index
{
    if (index == 0) {
       [self addReadNum:(VillageModel *)model];
    }
    else {
        [self addPolicyReadNum:(PolicyModel *)model];
    }
}

#pragma mark - 区域id
- (void)getAreaid:(LocationModel *)model
{
    [HomeHttpTools GetAreaIDWith:model Success:^(NSString *arearid){
        self.homeViews.areaIDStr = arearid;
    } failure:^(NSString *error) {
//        self.homeViews.areaIDStr = @"";
    }];
}

#pragma mark - 头条增加阅读数
- (void)addReadNum:(VillageModel *)model
{
    [MBProgressHUD showMessage:@"正在加载..." toView:self.navigationController.view];
    [HomeHttpTools AddHeadlineNewReadNumWith:model Success:^() {
        [MBProgressHUD hideHUDForView:self.navigationController.view];
        [Tools ToNextName:@"HeadLineDetailsController" Params:model CurrentVC:self Completion:^(id obj) {
            
        }];
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUDForView:self.navigationController.view];
    }];
}

#pragma mark - 政策法规 增加阅读数
- (void)addPolicyReadNum:(PolicyModel *)model
{
    WeakSelf(weakSelf);
    [MBProgressHUD showMessage:@"正在加载..." toView:self.navigationController.view];
    [PolicyHttpTools AddPolicyReadNumWithID:model.ID Success:^{
        [MBProgressHUD hideHUDForView:weakSelf.navigationController.view];
        [Tools ToNextName:@"PolicyDetailViewController" Params:model CurrentVC:weakSelf Completion:^(id obj) {
            
        }];
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUDForView:weakSelf.navigationController.view];
        [MBProgressHUD showError:error toView:weakSelf.navigationController.view];
    }];
}

#pragma mark - 升级
- (void)updateVersion
{
    UpdateVersion *updates = [[UpdateVersion alloc]init];
    updates.baseURL = UpdateURL;
    updates.currentVersion = [VersionCode floatValue] * 10;
    updates.appName = @"快处快赔";
    [updates checkVersion];
    updates.updateBlock = ^(BOOL isUpdate, BOOL IsUpgrade, NSString *remarks) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:remarks delegate:self cancelButtonTitle:nil otherButtonTitles:@"立即升级", nil];
        [alert show];
    };
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.baidu.com"]];
}
@end
